<%@ page language="java" contentType="text/html; charset=UTF-8"
pageEncoding="UTF-8"%>

<%@ include file="../../../../header.jsp"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
	<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
	<title>Insert title here</title>
	<style>
	[id^=gbox_]{
		margin:0 auto;
	}
	.ui-pg-input{
		text-align:center
	}
	#btn-search{
		background: #009fff;
		border: blue;
	}
	td select{
		width:50px;
	}
</style>
</head>
<body>

<div class="mod-toolbar-top cf" style="margin-top: 10px;">
	<div class="fr" style="margin-top: 30px;">
	    <a href="javascript:void();" class="ui-btn ui-btn-sp mrb"
		id="btn-addmore">批量添加</a>
		<a href="javascript:void();" class="ui-btn ui-btn-sp mrb"
		id="btn-add">新增</a> <a href="javascript:void();" class="ui-btn ui-btn-sp mrb"
		id="btn-search">搜索</a>
	</div>
</div>
<div class="grid-wrap" align="left" style="margin-left: 10px;" id="list2">
	<table id="grid1"></table>
	<div id="pager2"></div>
</div>
<div class="grid-wrap" align="left" style="margin-left: 10px;">
	<table id="grid2"></table>
</div>
<div class="grid-wrap" align="left" style="margin-left: 10px;">
	<table id="grid3"></table>
</div>
<script type="text/javascript">
	function update() {
		alert("update");
		$.ajax({
			url : "update",
			dataType : "json",
			success : function(data) {

			},
			erro : function() {
				alert("爆炸了！");
			}

		});
	}
	function deletes() {
		alert("update");
		$.ajax({
			url : "delete",
			dataType : "json",
			success : function(data) {

			},
			erro : function() {
				alert("爆炸了！");
			}
		});
	}
	function clearGrid() {
		$("#grid").jqGrid('clearGridData');
	}
		//添加图标
		function operationFormat(cellValue, options, rowObject) {
			return '<div class="operating" data-id="' + options.rowId + 
			'"><span class="ui-icon ui-icon-pencil" title="编辑" data-toggle="modal" data-target="#myModal"></span>&nbsp&nbsp'
			+ '<span class="ui-icon ui-icon-trash" title="删除"></span>';
		}
		var checkBoxIdGen=0;
		function booleanFormater(cellValue,option,rowObject){
			checkBoxIdGen++;
			return ' <input  class="switch-simple-3" id="'+option.colModel.name+'-'+checkBoxIdGen+'" type="checkbox" '+((cellValue==true)?'checked':'')+'><label for="'+option.colModel.name+'-'+checkBoxIdGen+'" class="green"></label>';
			//return '<div class="switch switch-mini"><input type="checkbox" '+((cellValue==true)?'checked':'')+' /></div>';
		}
		$(function() {


			$("#grid1").jqGrid(
			{
				url : "/getAllEmploy",
				mtype : "post",
				datatype : "json",
				height : 400,
				width : 1155,
				sortname : 'id',
				shrinkToFit : false,
				autoScroll : false,
				rownumbers : true,
				rowNum : -1,
				cellEdit : true,
				cellsubmit : "clientArray",
				colNames : [ '操作','公司名称', '用户编码', '用户名称', '密码', '邮箱',
				'手机', '微信','QQ','备注','创建时间','修改时间','使用标志'],
				colModel : [ {
					name : 'id',
					index : 'id',
					width : 70,
					formatter : operationFormat,
					align : "center",
					sortable : false
				},{
					name : 'companyname',
					index : 'companyname',
					align : "center",
								width : 80,
							searchoptions : {
								sopt : [ 'cn' ]
							},
						}, {
							name : 'usercode',
							index : 'usercode',
								width : 80,
							align : "center",
							editable : true,
							editoptions : {
								size : 10
							},
							searchoptions : {
								sopt : [ 'cn' ]
							},
						}, {
							name : 'username',
							index : 'username',
								width : 80,
							align : "center",
							editable : true,
							editoptions : {
								size : 10
							},
							search : false,
						}, {
							name : 'password',
							index : 'password',
								width : 80,
							align : "center",
							editable : true,
							hidden:true,
							editoptions : {
								size : 10
							},
							search : false,
						}, {
							name : 'email',
							index : 'email',
									width : 80,
							align : "center",
							editable : true,
							editoptions : {
								size : 10
							},
							search : false,
						}, {
							name : 'mobile',
							index : 'mobile',
								width : 80,
							editable : true,
							align : "center",

							editoptions : {
								size : 10
							},
							search : false,
						}, {
							name : 'weixin',
							index : 'weixin',
								width : 80,
							editable : true,
							align : "center",

							editoptions : {
								size : 10
							},
							search : false,
						},
						{
							name : 'qq',
							index : 'qq',
								width : 80,
							editable : true,
							align : "center",

							editoptions : {
								size : 10
							},
							search : false,
						},
						{
							name : 'remark',
							index : 'remark',
								width : 80,
							editable : true,
							align : "center",

							editoptions : {
								size : 10
							},
							search : false,
						},
						{
							name : 'createtime',
							index : 'createtime',
								width : 130,
							editable : true,
							align : "center",

							editoptions : {
								size : 10
							},
							search : false,
							formatter:function(value,options,row){return new Date(value).Format('yyyy-MM-dd hh:mm:ss');}
							
						},
						{
							name : 'updatetime',
							index : 'updatetime',
								width : 130,
							editable : true,
							align : "center",

							editoptions : {
								size : 10
							},
							search : false,
							formatter:function(value,options,row){
								if(value==null){
									return "";
								}
								return new Date(value).Format('yyyy-MM-dd hh:mm:ss');
								}
						},
						{
							name : 'active',
							index : 'active',
								width : 90,
							editable : false,
							align : "center",

							editoptions : {
								size : 10
							},
							search : false,
							formatter:booleanFormater
						}
						],
						rowNum : 5,//一页显示多少条
						rowList : [ 5,10,20 ],//可供用户选择一页显示多少条
						pager : '#pager2',//表格页脚的占位符(一般是div)的id
						viewrecords : true,
						loadComplete : function() {
							loadover();
						},
						gridComplete : function() {
						},
						afterInsertRow : function() {
						},
						beforeEditCell : function(rowid, cellname, v, iRow,
							iCol) {
							lastrow = iRow;
							lastcell = iCol;
						}
					});
			
			initTableRow();
			initEvent();
		})
		//设置每行的高度
		function loadover() {
			var grid = $("#grid1");
			var ids = grid.getDataIDs();
			for (var i = 0; i < ids.length; i++) {
				grid.setRowData(ids[i], false, {
					height : 35
				});
			}
		}
		function initTableRow() {
			for (var i = 1; i <= 4; i++) {
				jQuery("#grid1").jqGrid('addRowData', i, {});
			}
			loadover();
		}
		

		//日期格式化函数
		Date.prototype.Format = function (fmt) { 
			
		    var o = {  
		        "M+": this.getMonth() + 1, //月份   
		        "d+": this.getDate(), //日   
		        "h+": this.getHours(), //小时   
		        "m+": this.getMinutes(), //分   
		        "s+": this.getSeconds(), //秒   
		        "q+": Math.floor((this.getMonth() + 3) / 3), //季度   
		        "S": this.getMilliseconds() //毫秒   
		    };  
		    if (/(y+)/.test(fmt)) fmt = fmt.replace(RegExp.$1, (this.getFullYear() + "").substr(4 - RegExp.$1.length));  
		    for (var k in o)  
		    if (new RegExp("(" + k + ")").test(fmt)) fmt = fmt.replace(RegExp.$1, (RegExp.$1.length == 1) ? (o[k]) : (("00" + o[k]).substr(("" + o[k]).length)));  
		    return fmt;  
		} 
		
		//添加事件
		function initEvent() {
			$("#btn-addmore").click(function(a) {

				handle.operate("addmore");
			}),
			$("#btn-add").click(function(a) {

				handle.operate("add");
			}), $("#btn-search").click(function(a){
				handle.search();
			}),$("#grid1").on("click", ".operating .ui-icon-pencil",
			function(a) {
				var rowid = $(this).parent().data("id");
				var rowdata = $("#grid1").jqGrid("getRowData", rowid);
				handle.operate("edit", rowid, rowdata);
			}), $("#grid1").on("click", ".operating .ui-icon-trash",
			function(a) {
				a.preventDefault();
				var rowid = $(this).parent().data("id");
				handle.del(rowid);
			}), $("#btn-record").click(function(a) {
				$("#grid1").jqGrid().trigger("reloadGrid");
			}), $("#btn-setsession").click(function(a) {
				$.ajax({
					url : encodeURI("user/setsession"),
					type : "POST",
					success : function(data) {
						alert(data);
					}
				});
			}), $("#btn-getsession").click(function(a) {
				$.ajax({
					url : encodeURI("user/getsession"),
					type : "POST",
					success : function(data) {
						alert(data);
					}
				});
			})
		}
		handle = {
			operate : function(oper, rowid, rowdata) {
				var title;
				var data = {};
				if ("add" == oper) {
					title = "新增";
					data = {
						operate : oper,
						callback : this.callback
					}
					this.dialog = $.dialog({
						title : title,
						data : data,
						content : "url:/addjsp",
						width : 500,
						height : 250,
						max : false,
						min : false,
						cache : false,
						lock : true
					});
				}
				else if("addmore"==oper){
					title = "批量新增";
					data = {
						operate : oper,
						callback : this.callback
					}
					this.dialog = $.dialog({
						title : title,
						data : data,
						content : "url:/addmorejsp",
						width : 500,
						height : 250,
						max : false,
						min : false,
						cache : false,
						lock : true
					});
				}
				else {
					title = "修改";
					data = {
						operate : oper,
						rowdata : rowdata,
						rowid : rowid,
						callback : this.callback
					}
					this.dialog = $.dialog({
						title : title,
						data : data,
						content : "url:/editjsp",
						width : 500,
						height : 250,
						max : false,
						min : false,
						cache : false,
						lock : true
					});
				}
				
			},
			search:function(){
				var grid=$("grid1");
				this.dialog = $.dialog({
					title : "搜索",
					data : {
						callback:this.callback,
						grid:grid
					},
					content : "url:/searchjsp",
					width : 500,
					height : 250,
					max : false,
					min : false,
					cache : false,
					lock : true
				});
			},
			del : function(rowId) {
				$.dialog.confirm("删除的参数类型将不能恢复，请确认是否删除？", function() {
					/* Public.ajaxPost("category/deleteType", {
						id: rowId
					}, function(data) {
						alert(data);
						$("#grid1").jqGrid().trigger("reloadGrid");
					}); */
					$.ajax({
						url : encodeURI("/editEmploy?oper=del"),
						type : "POST",
						data : {
							"id" : rowId
						},
						success : function(data) {
							if(data.status==500){
								alert("该字段已被引用，不能删除");
							}
							$("#grid1").jqGrid().trigger("reloadGrid");
						}
						
					});
				});
			},
			callback : function(data) {
				if(data!=undefined){
					$('#grid1').jqGrid('setGridParam',{search:true});  
					$("#grid1").jqGrid("setGridParam",{postData:data}).jqGrid("setGridParam",{page:1}).trigger("reloadGrid");
				}else{
					$("#grid1").jqGrid().trigger("reloadGrid");
				}
			}
		};
	</script>
</body>
</html>